import { forwardRef, useEffect, useImperativeHandle } from "react";
import { Form, message } from "antd";
import moment from "moment";

import { IForm } from "@/components";
import { regExpMap } from "@/utils/consts";

import type { RecordItem } from "../type";
import type { FormItemConfig } from "@/components/FormItem";
import request from "@/utils/request";

interface Props {
	selRecord?: RecordItem;
}

const InfoForm = forwardRef((props: Props, ref) => {
	const [form] = Form.useForm();

	useEffect(() => {
		if (props.selRecord) {
			queryTenantDetail();
		}
	}, [props.selRecord]);

	useImperativeHandle(ref, () => ({
		validateFields: () => {
			return form
				.validateFields()
				.then(value => {
					return {
						...value
						// accountExpire: formatDateShowTime(value.accountExpire),
					};
				})
				.catch((error: any) => {
					message.warn("请完善租户基本信息");
					throw error;
				});
		}
	}));

	const queryTenantDetail = () => {
		// 租户详情接口
		request("tenantQueryById", { data: { tenantId: props.selRecord?.tenantId } }).then(res => {
			if (res.code === 0 && res.data) {
				const { accountExpire } = res.data;
				form.setFieldsValue({
					...res.data
					// accountExpire: momentDate(accountExpire)
				});
			}
		});
	};

	const formItemsConfig: FormItemConfig[] = [
		{
			type: "input",
			formItemProps: {
				label: "系统名称",
				name: "tenantName",
				rules: [{ required: true }]
			}
		},
		{
			type: "input",
			formItemProps: {
				label: "联系人",
				name: "phone"
				// rules: [{ required: true }, { pattern: regExpMap.mobile, message: "请输入正确手机号" }]
			}
		}
	];

	return <IForm layout="vertical" form={form} formItemsConfig={formItemsConfig}></IForm>;
});

InfoForm.displayName = "InfoForm";

export default InfoForm;
